/// Manage Deck Panel ///

using System.Windows.Forms;
using System.Drawing;
using System.Net;
using System.Threading;
using ajma.Utils;
using System;
using System.IO;
using System.Collections;

namespace Xyne
{
	public class ManageDecksPanel : Panel
	{
		private const string DECK_FOLDER = "decks/";
		private Label deckLabel;
		private ListBox deckList;
		private Button addButton;
		private Button manageButton;
		private Button removeButton;
		private Button back;
		private ButtonClicked callback;
		private EditDeck edit;
		
		private Hashtable decks = new Hashtable();
		
		public delegate void ButtonClicked();
		public delegate void EditDeck(string filename);
		
		public ButtonClicked BackCallback
		{
			get { return callback; }
			set { callback = value; }
		}
		
		public EditDeck EditCallback
		{
			get { return edit; }
			set { edit = value; }
		}
		
		public ManageDecksPanel()
		{
			ConstructControls();
			
			string[] files = Directory.GetFiles(DECK_FOLDER);
			
			foreach( string filename in files )
			{
				try
				{
					FileInfo file = new FileInfo(filename);
					StreamReader reader = file.OpenText();
					
					string deckName = reader.ReadLine();
					
					reader.Close();
					
					deckList.Items.Add(deckName);
					decks.Add(deckName, filename);
				}
				catch( Exception ) {}
			}
		}
		
		private void ConstructControls()
		{
			SuspendLayout();
			
			int height = 0;
			
			deckLabel = new Label();
			deckLabel.Text = "Decks";
			deckLabel.Location = new Point(5,5);
			Controls.Add(deckLabel);
			height += deckLabel.Size.Height + 5;
			
			deckList = new ListBox();
			deckList.HorizontalScrollbar = true;
			deckList.Location = new Point(5,height);
			deckList.Size = new Size(150,200);
			Controls.Add(deckList);
			height += deckList.Size.Height + 5;
			
			height += 30;
			
			back = new Button();
			back.Text = "Back";
			back.Location = new Point(deckList.Location.X + deckList.Width + 5,height - 60);
			back.Click += new EventHandler(BackClicked);
			Controls.Add(back);
			
			addButton = new Button();
			addButton.Text = "Add";
			addButton.Location = new Point(deckList.Location.X + deckList.Width + 5,deckList.Location.Y);
			addButton.Click += new EventHandler(AddClicked);
			Controls.Add(addButton);
			
			manageButton = new Button();
			manageButton.Text = "Edit";
			manageButton.Location = new Point(deckList.Location.X + deckList.Width + 5,addButton.Location.Y + addButton.Size.Height + 5);
			manageButton.Click += new EventHandler(EditClicked);
			Controls.Add(manageButton);
			
			removeButton = new Button();
			removeButton.Text = "Remove";
			removeButton.Location = new Point(deckList.Location.X + deckList.Width + 5,manageButton.Location.Y + manageButton.Size.Height + 5);
			removeButton.Click += new EventHandler(RemoveClicked);
			Controls.Add(removeButton);
			
			ClientSize = new Size(back.Location.X + back.Size.Width + 10, height);
			
			ResumeLayout();
		}
		
		private void AddClicked(object sender, EventArgs e)
		{
			string name = InputBox.Show("Please enter a deck name.","Deck Name");
			
			if( deckList.Items.Contains(name) )
			{
				MessageBox.Show("Please enter a unique name");
			}
			
			if( name != "" && !deckList.Items.Contains(name) )
			{
				int possible = 0;
				while( File.Exists(DECK_FOLDER + possible.ToString()) ) possible++;
				
				FileInfo file = new FileInfo(DECK_FOLDER + possible.ToString());
				StreamWriter write = file.CreateText();
				
				write.WriteLine(name);
				write.Close();
				
				deckList.Items.Add(name);
				decks.Add(name, file.FullName);
			}
		}
		
		private void RemoveClicked(object sender, EventArgs e)
		{
			if( deckList.SelectedItem != null )
			{
				string name = deckList.SelectedItem.ToString();
			
				try
				{
					deckList.Items.Remove(name);
					
					File.Delete(decks[name].ToString());
					
					decks.Remove(name);
				}
				catch( Exception ) {}
			}
		}
		
		private void EditClicked(object sender, EventArgs e)
		{
			if( deckList.SelectedItem != null )
			{
				edit(decks[deckList.SelectedItem.ToString()].ToString());
			}
		}
		
		private void BackClicked(object sender, EventArgs e)
		{
			callback();
		}
	}
}